import 'package:camera/camera.dart';
import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/main.dart';
import 'package:flutter/material.dart';

class TakePic extends StatefulWidget {
  List<String> levels;
  TakePic({
    super.key,
    required this.levels,
  });

  @override
  State<TakePic> createState() => _TakePicState();
}

class _TakePicState extends State<TakePic> {
  late CameraController controller;
  double aspectRatio = 1.3333;

  @override
  void initState() {
    super.initState();
    if (cameras.length > 0) {
      controller = CameraController(cameras[0], ResolutionPreset.max);
      controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        aspectRatio = controller.value.aspectRatio;
        setState(() {});
      }).catchError((Object e) {
        if (e is CameraException) {
          switch (e.code) {
            case 'CameraAccessDenied':
              break;
            default:
              break;
          }
        }
      });
    }
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (cameras.length == 0) {
      return Text("没有可用的摄像头");
    }
    if (!controller.value.isInitialized) {
      return Scaffold();
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "请拍摄",
          style: XSTextStyle(
            context: context,
            color: os_black,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        leading: IconButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          icon: Icon(Icons.chevron_left),
        ),
      ),
      body: ListView(
        children: [
          Center(
            child: Text(
              "${widget.levels.join(" → ")}",
              textAlign: TextAlign.center,
              style: XSTextStyle(
                context: context,
                color: os_light_light_dark_card,
                fontSize: 16,
                fontWeight: FontWeight.normal,
              ),
            ),
          ),
          Container(height: 20),
          Container(
            margin: EdgeInsets.only(left: 15, right: 15),
            width: MediaQuery.of(context).size.width - 30,
            height: (MediaQuery.of(context).size.width - 30) * aspectRatio,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(25),
              child: CameraPreview(controller),
            ),
          ),
          // Text(aspectRatio.toString()),
          SafeArea(
            child: Container(
              margin: EdgeInsets.only(top: 30, bottom: 20),
              width: 200,
              child: Center(
                child: IconButton(
                  onPressed: () {
                    controller.takePicture().then((XFile file) {
                      controller.pausePreview();
                      Navigator.of(context)
                          .pushNamed("/process_img", arguments: {
                        "file": file,
                        "levels": widget.levels,
                      }).then((_) {
                        controller.resumePreview();
                      });
                    });
                  },
                  style: ButtonStyle(
                    backgroundColor: WidgetStatePropertyAll(os_color),
                    padding: WidgetStatePropertyAll(EdgeInsets.all(10)),
                  ),
                  icon: Container(
                    padding: EdgeInsets.all(10),
                    child: Icon(
                      Icons.camera_alt,
                      size: 30,
                      color: os_white,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
